package com.cashoutgaming.client.modules.toolbar;

import com.cashoutgaming.client.modules.toolbar.button.ApplicationToolbarButton;
import com.cashoutgaming.client.modules.toolbar.button.LogoToolbarButton;
import com.cashoutgaming.client.modules.toolbar.button.TextToolbarButton;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.ui.HTML;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.core.client.util.Padding;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer;
import com.sencha.gxt.widget.core.client.menu.Item;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.sencha.gxt.widget.core.client.menu.MenuItem;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;

/**
 * This is the application toolbar desktop view.
 * 
 */
public class ApplicationToolbarView extends ViewWithUiHandlers<ApplicationToolbarUiHandlers> implements ApplicationToolbarPresenter.MyView {

	private ApplicationToolbarButton userButton;
	// private TextToolbarButton companyName;
	private final ApplicationDesktopToolbarStyle style;
	private HBoxLayoutContainer toolBar;
	private LogoToolbarButton perceptiveButton;
	private Long userId;
	private TextToolbarButton homeBtn, leaderBtn, streamsBtn, mfinderBtn, supportBtn;

	interface ApplicationDesktopToolbarStyle extends CssResource {

		String banner_bar();
	}

	interface StyleResources extends ClientBundle {

		@Source("ApplicationDesktopToolbarCss.css")
		ApplicationDesktopToolbarStyle css();
	}

	interface ApplicationDesktopToolbarCssTemplate extends XTemplates {

		@XTemplate(source = "ApplicationDesktopToolbarCssTemplate.txt")
		SafeHtml generateCustomCss(String mainMenuBgImage, String mainMenuBorderColor);
	}

	@Inject
	public ApplicationToolbarView() {
		final StyleResources resources = GWT.create(StyleResources.class);
		resources.css().ensureInjected();
		style = resources.css();
		buildToolbar();
		this.initWidget(toolBar);
	}

	// this simple north container does no RPC so its simply a widget
	public void buildToolbar() {
		toolBar = new ToolBar();
		toolBar.setHeight(50);
		// toolBar.setSpacing(5);
		toolBar.setPadding(new Padding(5));
		toolBar.removeStyleName("x-toolbar-mark");
		toolBar.setStylePrimaryName(style.banner_bar());

		perceptiveButton = new LogoToolbarButton(true);
//		perceptiveButton.setIcon(CommonToolImages.INSTANCE.getBO2Logo());
		// find logo
		/*
		 * perceptiveButton.addClickHandler(new ClickHandler() {
		 * 
		 * @Override public void onClick(ClickEvent event) {
		 * getUiHandlers().revealAbout(); } });
		 */
		Menu helpMenu = new Menu();
		perceptiveButton.setMenu(helpMenu);

//		toolBar.add(perceptiveButton);

		BoxLayoutData flex = new BoxLayoutData(new Margins(0, 5, 0, 0));
		flex.setFlex(1);
		toolBar.add(new HTML("<div></div>"), flex);

		homeBtn = new TextToolbarButton("Home");
		homeBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				getUiHandlers().goHome();
				homeBtn.setActive(true);
				leaderBtn.setActive(false);
				mfinderBtn.setActive(false);
				streamsBtn.setActive(false);
				supportBtn.setActive(false);
			}

		});
		toolBar.add(homeBtn);

		leaderBtn = new TextToolbarButton("Leaderboards");
		leaderBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				homeBtn.setActive(false);
				leaderBtn.setActive(true);
				mfinderBtn.setActive(false);
				streamsBtn.setActive(false);
				supportBtn.setActive(false);
			}

		});
		toolBar.add(leaderBtn);

		mfinderBtn = new TextToolbarButton("Match Finder");
		mfinderBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				homeBtn.setActive(false);
				leaderBtn.setActive(false);
				mfinderBtn.setActive(true);
				streamsBtn.setActive(false);
				supportBtn.setActive(false);
			}

		});
		toolBar.add(mfinderBtn);

		streamsBtn = new TextToolbarButton("Streams");
		streamsBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				homeBtn.setActive(false);
				leaderBtn.setActive(false);
				mfinderBtn.setActive(false);
				streamsBtn.setActive(true);
				supportBtn.setActive(false);
			}

		});
		toolBar.add(streamsBtn);

		supportBtn = new TextToolbarButton("Support");
		supportBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				homeBtn.setActive(false);
				leaderBtn.setActive(false);
				mfinderBtn.setActive(false);
				streamsBtn.setActive(false);
				supportBtn.setActive(true);
			}

		});
		toolBar.add(supportBtn);

		toolBar.add(new HTML("<div></div>"), flex);

		userButton = new ApplicationToolbarButton("Logged in User", true);
		// userButton.setIcon(CommonToolImages.INSTANCE.getProfile());TODO add
		// icons
		// BoxLayoutData flex = new BoxLayoutData(new Margins(0, 5, 0, 0));
		// flex.setFlex(1);
		// toolBar.add(userButton, flex);
		toolBar.add(userButton);

		Menu userMenu = new Menu();
		MenuItem profile = new MenuItem("My Profile");
		profile.addSelectionHandler(new SelectionHandler<Item>() {

			@Override
			public void onSelection(SelectionEvent<Item> event) {
				getUiHandlers().showUserProfile(userId);

			}
		});
		userMenu.add(profile);
		userButton.setMenu(userMenu);

		MenuItem logout = new MenuItem("Sign Out");
		logout.addSelectionHandler(new SelectionHandler<Item>() {

			@Override
			public void onSelection(SelectionEvent<Item> event) {
				logout();
			}
		});

		// logout.setIcon(CommonToolImages.INSTANCE.getSignout());TODO get
		// images
		userMenu.add(logout);
	}

	public void logout() {
		getUiHandlers().signout();
	}

	@Override
	public void updateUser(String username) {
		String display = username;
		if(username==null || username.trim().length()>0){
			display = "Login";
		}
		userButton.setText(display);
		toolBar.forceLayout();

	}

	@Override
	public void refreshLayout() {
		toolBar.forceLayout();
	}

	@Override
	public void setUserId(Long id) {
		this.userId = id;
	}
}
